package cn.jason.basic.service.impl;

import cn.jason.basic.Constants.BaseConstant;
import cn.jason.basic.dto.RegisterDto;
import cn.jason.basic.exception.BusinessException;
import cn.jason.basic.service.IveridyCodeService;
import cn.jason.basic.util.SmsUtils;
import cn.jason.basic.util.StrUtils;
import cn.jason.basic.util.VerifyCodeUtils;
import cn.jason.user.domain.User;
import cn.jason.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

@Service
public class veridyCodeServiceImpl implements IveridyCodeService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserMapper userMapper;

    @Override
    public String ImageCode(String key) {
        if(!StringUtils.hasLength(key)){
           throw new BusinessException("key值不能为空!!");
        }
        String code = StrUtils.getComplexRandomString(4);
        redisTemplate.opsForValue().set(key,code,5, TimeUnit.MINUTES);

        return VerifyCodeUtils.VerifyCode(100,38,code);
    }

    @Override
    public void smsCode(RegisterDto dto) {
        String phone = dto.getPhone();
        String imageCodeKey = dto.getImageCodeKey();
        String imageCodeValue = dto.getImageCodeValue();
        //空校验
        if(StringUtils.isEmpty(phone) || StringUtils.isEmpty(imageCodeKey) || StringUtils.isEmpty(imageCodeValue)){
            throw new BusinessException("参数不能为空!!");
        }
        //校验图形验证码
        Object imageCodeTmp = redisTemplate.opsForValue().get(imageCodeKey);
        //图形验证码失效
        if(imageCodeTmp == null){
            throw new BusinessException("验证码失效，请重新获取");
        }
        //图形验证码不正确
        if(!imageCodeTmp.toString().equalsIgnoreCase(imageCodeValue)){
            throw new BusinessException("验证码错误，请重新输入！");
        }
        //校验手机号是否注册
        User userTmp = userMapper.loadByPhone(phone);
        if(userTmp != null){
            throw new BusinessException("该手机号已经被注册，请直接登陆");
        }

        String key = BaseConstant.RegisterConstants.REGISTER_SMS_PREFIX + phone;
        Object smsCodeTmp = redisTemplate.opsForValue().get(key);
        String smsCode="";
        if(smsCodeTmp!=null){
            //校验是否过了重发时间
            Long oldTime = Long.valueOf(smsCodeTmp.toString().split(":")[1]);
            if(System.currentTimeMillis()-oldTime <= 1*60*1000){
                throw new BusinessException("请你吃耳巴子！！！");
            }
            //大于一分钟 验证码还没过期 刷新验证时间
            smsCode = smsCodeTmp.toString().split(":")[0];
        }else{
            //生成新的短信验证码
             smsCode = StrUtils.getComplexRandomString(4);
        }
        //存储到redis 设置一个过期时间
        String smsCodeValue = smsCode + ":" + System.currentTimeMillis();
        redisTemplate.opsForValue().set(key,smsCodeValue,3,TimeUnit.MINUTES);
        //发送短信
        //SmsUtils.sendSms(phone,"尊敬的摩纳哥公国元首，你的邀请码是"+smsCode+"三分钟之内有效！");
        System.out.println("尊敬的摩纳哥公国元首，你的邀请码是"+smsCode+"三分钟之内有效");
    }
}
